package com.rndemo.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class UtilDate {
    //得到当前时间
    public static SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss",Locale.getDefault());
    /**
     * 时间字符串 转 想要格式的字符串(sql产生的带有 T Z 的也可转)
     *
     * @param strDate
     * 				时间字符串,为null时得到当前时间
     * @param style
     * 				为空时默认"yy-MM-dd HH:mm:ss"
     * @return: String
     * 				错误时返回 "yy-MM-dd HH:mm:ss"
     */
    public static String getTime(String strDate,String style) {
        try {
            if (strDate != null) {
                strDate = strDate.replace('T', ' ');
                strDate = strDate.replace('Z', '0');
            }
            if(style != null){
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.getDefault());
                if (strDate == null) {
                    return format.format(getDate());
                }else {
                    Date parse = format.parse(strDate);
                    format.applyPattern(style);
                    format = new SimpleDateFormat(style,Locale.getDefault());
                    return format.format(parse);
                }
            }else {
                if (strDate == null) {
                    return formatter.format(getDate());
                }else {
                    return formatter.format(formatter.parse(strDate));
                }
            }
        } catch (ParseException e) {
            Mlog.err(e);
        }
        return "yy-MM-dd HH:mm:ss";
    }
    /**
     * 时间字符串 转 想要格式的字符串(sql产生的带有 T Z 的也可转)
     *
     * @param style
     * 				为空时默认"yy-MM-dd HH:mm:ss"
     * @return: String
     * 				错误时返回 "yy-MM-dd HH:mm:ss"
     */
    public static String getTime(Date date,String style) {
        if (style != null) {
            SimpleDateFormat format = new SimpleDateFormat(style, Locale.getDefault());
            if (date == null) {
                return format.format(getDate());
            } else {
                return format.format(date);
            }
        }else {
            if (date == null) {
                return formatter.format(getDate());
            } else {
                return formatter.format(date);
            }
        }
    }
    /**
     * 得到当前Date
     * @return: Date
     */
    public static Date getDate(){
        return Calendar.getInstance().getTime();
    }
    public static Date getDate(String str,String type){
        if (type == null) {
            type = "yyyy-MM-dd HH:mm:ss";
        }
        try {
            return new SimpleDateFormat(type, Locale.getDefault()).parse(str);
        } catch (Exception e) {
            Mlog.err(e);
        }
        return null;
    }
    /**
     *  将时间加上xxx小时
     * @param date 如果为空表示当前时间
     * @return: Date
     */
    public static Date getDateH(Date date,int n){
        Calendar c = Calendar.getInstance();
        if (date==null) {
            c.setTime(getDate());
        }else {
            c.setTime(date);
        }
        c.add(Calendar.HOUR_OF_DAY, n);
        return c.getTime();
    }

    /**
     * 将时间加上xxx分钟
     */
    public static Date getDateM(Date date,int n){
        Calendar c = Calendar.getInstance();
        if (date==null) {
            c.setTime(getDate());
        }else {
            c.setTime(date);
        }
        c.add(Calendar.MINUTE, n);
        return c.getTime();
    }

    /**
     * 得到时间
     * @param timeMs 千秒
     * @return
     * @return: String
     */
    public static String getTime(long timeMs){
        int totalSeconds = (int)(timeMs / 1000);
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;
        return hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes, seconds) : String.format("%02d:%02d", minutes, seconds);
    }
    /**
     * 根据年月日获取年龄
     *
     * @param year
     *            年
     * @param month
     *            月
     * @param day
     *            日
     * @return
     */
    public static int getAge(int year, int month, int day) {
        int age;
        Calendar calendar = Calendar.getInstance();
        if (calendar.get(Calendar.YEAR) == year) {
            if (calendar.get(Calendar.MONTH) == month) {
                if (calendar.get(Calendar.DAY_OF_MONTH) >= day) {
                    age = calendar.get(Calendar.YEAR) - year + 1;
                } else {
                    age = calendar.get(Calendar.YEAR) - year;
                }
            } else if (calendar.get(Calendar.MONTH) > month) {
                age = calendar.get(Calendar.YEAR) - year + 1;
            } else {
                age = calendar.get(Calendar.YEAR) - year;
            }
        } else {
            age = calendar.get(Calendar.YEAR) - year;
        }
        if (age < 0) {
            return 0;
        }
        return age;
    }
    /**
     * 根据月日获取星座
     *
     * @param month
     *            月
     * @param day
     *            日
     * @return
     */
    public static String getConstellation(int month, int day) {
        if ((month == 1 && day >= 20) || (month == 2 && day <= 18)) {
            return "水瓶座";
        } else if ((month == 2 && day >= 19) || (month == 3 && day <= 20)) {
            return "双鱼座";
        } else if ((month == 3 && day >= 21) || (month == 4 && day <= 19)) {
            return "白羊座";
        } else if ((month == 4 && day >= 20) || (month == 5 && day <= 20)) {
            return "金牛座";
        } else if ((month == 5 && day >= 21) || (month == 6 && day <= 21)) {
            return "双子座";
        } else if ((month == 6 && day >= 22) || (month == 7 && day <= 22)) {
            return "巨蟹座";
        } else if ((month == 7 && day >= 23) || (month == 8 && day <= 22)) {
            return "狮子座";
        } else if ((month == 8 && day >= 23) || (month == 9 && day <= 22)) {
            return "处女座";
        } else if ((month == 9 && day >= 23) || (month == 10 && day <= 23)) {
            return "天秤座";
        } else if ((month == 10 && day >= 24) || (month == 11 && day <= 22)) {
            return "天蝎座";
        } else if ((month == 11 && day >= 23) || (month == 12 && day <= 21)) {
            return "射手座";
        } else if ((((month != 12) || (day < 22))) && (((month != 1) || (day > 19)))) {
            return "魔蝎座";
        }
        return "";
    }

}
